/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var threeSumClosest = function (nums, target) {
  let n = nums.length
  let sum = 0
  let oldDiff = Number.MAX_SAFE_INTEGER
  for (let i = 0; i < n-2; i++) {
    for (let j = i+1; j < n-1; j++) {
      for (let k = j+1; k < n; k++) {
        let value = nums[i] + nums[j] + nums[k]
        
        let diff
        if (value > target) {
          diff = value - target
        }else{
          diff = target - value
        }
        if(diff < oldDiff){
          oldDiff = diff
          sum = value
        }
      }
      
    }
    
  }
  return sum

};

var nums = [-1, 2, 1, -4],target = 1

console.log(threeSumClosest(nums,target))
